package com.smz.controller;

import com.smz.entity.User;
import com.smz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {


    @Autowired
    private UserService userService;

    @GetMapping("findAllUsers")
    public ResponseEntity<List<User>> findAll(){
        List<User> users = userService.selectAllUsers();
        return ResponseEntity.ok(users);
    }
    @PostMapping("findById/{id}")
    public ResponseEntity<User>findByIdUser(@PathVariable("id")Integer id){
        User byId = userService.findById(id);
        return ResponseEntity.ok(byId);
    }
    @PostMapping("user")
    public ResponseEntity<String>addUser(@RequestBody User user){
        userService.addUser(user);
        return ResponseEntity.ok("ok");
    }

    @PutMapping("user")
    public ResponseEntity<String>updateUser(@RequestBody User user){
        userService.updateUser(user);
        return ResponseEntity.ok("ok");
    }

    @DeleteMapping("user/{id}")
    public ResponseEntity<String>deleteUser(@PathVariable("id")Integer id){
        userService.deleteUser(id);
        return ResponseEntity.ok("ok");
    }
}
